package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzapi.common.controller.ApittController;
import com.jinzhi.jzweb.domain.ExaminationResultsComDO;
import com.jinzhi.jzweb.domain.StuSignUpComDO;
import com.jinzhi.jzweb.pojo.ExaminationAnswerSheetComOneExlVO;
import com.jinzhi.jzweb.pojo.StuSignUpTeamExlVo;
import com.jinzhi.jzweb.pojo.StuSignUpTeamVo;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import com.jinzhi.jzweb.utils.FractionUtil;
import com.jinzhi.sys.service.UserRoleService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 超级管理员 总裁篇查看分数
 * <pre>
 * 团队赛场查看分数 统计
 * 2023
 * </pre>
 * <small> 2023-05-05 15:11:09 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/examinationAnswerSheetComTeamSour")
public class ExaminationAnswerSheetComTeamSourController extends BaseController {
    @Autowired
    private StuSignUpComService stuSignUpService;

    /**
     * 团队赛场成绩统计导出
     *
     * @param response
     */
    @GetMapping("/downloadImportStu")
    @RequiresPermissions("jzweb:stuSignUpCom:import")
    @ResponseBody
    public void downloadImportTemplate(HttpServletResponse response, ExaminationResultsComDO examinationAnswerSheetComDT) {
        if(!StringUtils.isNotBlank(examinationAnswerSheetComDT.getUnitName())){
            examinationAnswerSheetComDT.setUnitName(null);
        }
        List<StuSignUpTeamVo> mapList=stuSignUpService.sumTeamStuSignExam(examinationAnswerSheetComDT.getUnitName());
        List<StuSignUpTeamVo> mapLists= FractionUtil.teamList(mapList);

        int i=1;
        List<StuSignUpTeamExlVo> mapListsS=new ArrayList<>();
        for(StuSignUpTeamVo li:mapLists){
            StuSignUpTeamExlVo stuSignUpTeamExlVo=new StuSignUpTeamExlVo();
            stuSignUpTeamExlVo.setNum(i);
            List<StuSignUpTeamVo>  voList=  stuSignUpService.sumOneStuSignExam(li.getUnitName());
            List<StuSignUpTeamVo> mapLists1= FractionUtil.teamList(voList);
            mapLists1.sort((a,b) -> b.getTeamTotal().compareTo(a.getTeamTotal()));
            if(mapLists1.size()==1){
                stuSignUpTeamExlVo.setOneSum(mapLists1.get(0).getTeamTotal());
                stuSignUpTeamExlVo.setName1(mapLists1.get(0).getName());
            }else if(mapLists1.size()==2){
                stuSignUpTeamExlVo.setOneSum(mapLists1.get(0).getTeamTotal());
                stuSignUpTeamExlVo.setName1(mapLists1.get(0).getName());
                stuSignUpTeamExlVo.setTwoSum(mapLists1.get(1).getTeamTotal());
                stuSignUpTeamExlVo.setName2(mapLists1.get(1).getName());
            }
            stuSignUpTeamExlVo.setTeamTotal(li.getTeamTotal());
            stuSignUpTeamExlVo.setUnitName(li.getUnitName());
            i++;
            mapListsS.add(stuSignUpTeamExlVo);
        }

        //导出操作
        EasyPoiUtil.exportExcel(mapListsS, "团队赛统计成绩导出", "团队赛统计成绩导出", StuSignUpTeamExlVo.class, "团队赛统计成绩导出", response);
    }



    /**
     * 超级管理员 一级管理员查看分数
     * @param model
     * @return
     */
    @GetMapping()
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:examinationAnswerSheetCom")
    String info( Model model) {
        return "jzweb/examinationAnswerSheetCom/examinationAnswerSheetComTeamSour";
    }


    /**
     * 团队赛场理论成绩统计
     * @param examinationAnswerSheetComDTO
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("jzweb:examinationAnswerSheetCom:examinationAnswerSheetComSour")
    public Result<Page<StuSignUpTeamVo>> list(ExaminationResultsComDO examinationAnswerSheetComDTO){
        if(!StringUtils.isNotBlank(examinationAnswerSheetComDTO.getUnitName())){
            examinationAnswerSheetComDTO.setUnitName(null);
        }
        List<StuSignUpTeamVo> mapList=stuSignUpService.sumTeamStuSignExam(examinationAnswerSheetComDTO.getUnitName());
        List<StuSignUpTeamVo> mapLists= FractionUtil.teamList(mapList);

        int i=1;
        for(StuSignUpTeamVo li:mapLists){
            li.setNum(i);
            List<StuSignUpTeamVo>  voList=  stuSignUpService.sumOneStuSignExam(li.getUnitName());
            List<StuSignUpTeamVo> mapLists1= FractionUtil.teamList(voList);
            mapLists1.sort((a,b) -> b.getTeamTotal().compareTo(a.getTeamTotal()));
                if(mapLists1.size()==1){
                    li.setOneSum(mapLists1.get(0).getTeamTotal());
                    li.setName1(mapLists1.get(0).getName());
                }else if(mapLists1.size()==2){
                    li.setOneSum(mapLists1.get(0).getTeamTotal());
                    li.setName1(mapLists1.get(0).getName());
                    li.setTwoSum(mapLists1.get(1).getTeamTotal());
                    li.setName2(mapLists1.get(1).getName());
                }
            i++;
        }

        Page<StuSignUpTeamVo> page1 =new Page<>();
        page1.setTotal(mapLists.size());
        page1.setSize(mapLists.size());
        page1.setCurrent(1);

        page1.setRecords(mapLists);
        return Result.ok(page1);
    }




}
